package com.xm.zcf.service.category.impl;

import com.xm.zcf.dao.category.CategoryDao;
import com.xm.zcf.domain.category.Category;
import com.xm.zcf.service.category.CategoryException;
import com.xm.zcf.service.category.CategoryService;
import com.xm.zcf.service.categorySecond.CategorySecondService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/3/13.
 */
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryDao categoryDao;
    @Resource
    private CategorySecondService categorySecondService;

    @Override
    public List<Category> findAll() {
        return categoryDao.findAll();
    }

    @Override
    public Category findById(Integer cid) {
        return categoryDao.findById(cid);
    }

    @Override
    public void deleteCategory(Category category) throws CategoryException{
        List csList=categorySecondService.findByCid(category.getCid());
        if(csList.size()>0){
            throw new CategoryException("一级分类下有二级分类，不能删除");
        }
        categoryDao.deleteCategory(category);
    }

    @Override
    public void save(Category model) {
        categoryDao.save(model);
    }

    @Override
    public void update(Category category) {
        categoryDao.update(category);
    }
}

